classdef TowFleischer < Cell
    properties
        Wp;
        Wz;
        Qu;
        Ga;
    end
    
    methods 
        function this = TowFleischer(poles, zeros, g)
            this.Name = 'Tow Fleischer';
            this.Poles = poles;
            this.Zeros = zeros;
            this.Gain = g;
            this.ImagePath='Stages Schematics/TF.PNG';
            wp = abs(poles(1));
            wz = abs(zeros(1));
            [alpha ~] = cart2pol(real(poles(1)), imag(poles(1)));
            Q = abs(1./(2*sin(abs(alpha-pi/2))));
            this.calcCell(wp,wz,Q,g);
        end
        
        function status=calcCell(obj,wp,wz,Q,g)
            
            obj.Wp=wp;
            obj.Wz=wz;
            obj.Qu=Q;
            obj.Ga=g;
            
            caux=10e-9;
            zinp=47e3;
            
            cap(1)=caux;
            cap(2)=caux;
            
            resist(3)=zinp;
            resist(7)=zinp;
            resist(8)=zinp;            
            resist(6)=resist(8)*g;
            
            resist(2)=Q*sqrt(g)/(wp*cap(1));
            resist(4)=resist(2);
            resist(1)=1/(((wz*cap(1))^2)*resist(8)*resist(4)/resist(7));
            %resist(5)=((wp*cap(1))^2)*resist(4)*resist(8)*resist(1)
            resist(5)=((wp*cap(1))^2)*(resist(4)*resist(8)*resist(1));
            %also is equal to: resist(5)=((Q/resist(2))^2)*resist(4)*resist(6)*resist(1)
            
            obj.Resists = resist;
            obj.Caps = cap;
            
            %Sensibilidad de wp
            senswp(1) = -0.5;
            senswp(2) = 0;
            senswp(3) = 0;
            senswp(4) = -0.5;
            senswp(5) = 0.5;
            senswp(6) = -0.5;
            senswp(7) = 0;
            senswp(8) = 0;
            senswp(9) = -0.5;
            senswp(10) = -0.5;
            %Sensibilidad de wz
            senswz(1) = -0.5;
            senswz(2) = 0;
            senswz(3) = 0;
            senswz(4) = -0.5;
            senswz(5) = 0;
            senswz(6) = 0;
            senswz(7) = 0.5;
            senswz(8) = -0.5;
            senswz(9) = -0.5;
            senswz(10) = -0.5;
            %Sensibilidad de Q
            sensQ(1) = -0.5;
            sensQ(2) = 1;
            sensQ(3) = 0;
            sensQ(4) = -0.5;
            sensQ(5) = 0.5;
            sensQ(6) = -0.5;
            sensQ(7) = 0;
            sensQ(8) = 0;
            sensQ(9) = 0.5;
            sensQ(10) = -0.5;
            %Sensibilidad GBS
            sensG(1) = 0;
            sensG(2) = 0;
            sensG(3) = 0;
            sensG(4) = 0;
            sensG(5) = 0;
            sensG(6) = 1;
            sensG(7) = 0;
            sensG(8) = -1;
            sensG(9) = 0;
            sensG(10) = 0;
            sensG(11) = 0;
            
            obj.Senswp = senswp;
            obj.Senswz = senswz;
            obj.SensQ = sensQ;
            obj.SensG = sensG;
            
            status = obj.validateComponents(resist,cap);
        end
        
        function status=componentChange(obj,component,value)

            BackUPR=obj.Resists;
            BackUPC=obj.Caps;
            switch (component)
                case 1
                    caux=10e-9;
                    zinp=47e3;

                    cap(1)=caux;
                    cap(2)=cap(1);
                    
                    resist(3)=zinp;
                    resist(7)=zinp;
                    resist(8)=zinp;       
                    resist(6)=resist(8)*obj.Ga;
                    
                    resist(2)=obj.Qu*sqrt(obj.Ga)/(obj.Wp*cap(1));
                    resist(1)=value;
                    resist(4)=resist(2);
                    %resist(4)=resist(7)/(((obj.Wz*caux)^2)*resist(1)*resist(8));
                    resist(5)=((obj.Qu/resist(2))^2)*resist(1)*resist(4)*resist(6);
                case {2, 4}                  
                    zinp=47e3;

                    resist(3)=zinp;
                    resist(7)=zinp;
                    resist(6)=value;       
                    resist(8)=resist(6)/obj.Ga;

                    resist(2)=value;
                    resist(4)=resist(2);
                    cap(1)=obj.Qu*sqrt(obj.Ga)/(obj.Wp*resist(2));
                    cap(2)=cap(1);
                    
                    resist(1)=1/(((obj.Wz*cap(1))^2)*resist(8)*resist(4)/resist(7));
                    resist(5)=((obj.Wp*cap(1))^2)*(resist(4)*resist(8)*resist(1));
                case 5
                    caux=10e-9;
                    zinp=47e3;

                    cap(1)=caux;
                    cap(2)=cap(1);
                    
                    resist(3)=zinp;
                    resist(7)=zinp;
                    resist(8)=zinp;       
                    resist(6)=resist(8)*obj.Ga;
                    
                    resist(2)=obj.Qu*sqrt(obj.Ga)/(obj.Wp*cap(1));
                    resist(5)=value;

                    resist(4)=resist(2);
                    resist(1)=resist(5)/(((obj.Qu/resist(2))^2)*resist(4)*resist(6));
                case 6
                    zinp=47e3;
                    caux=10e-9;
                    cap(1)=caux;
                    cap(2)=cap(1);

                    resist(3)=zinp;
                    resist(7)=zinp;
                    resist(6)=value;       
                    resist(8)=resist(6)/obj.Ga;

                    resist(2)=obj.Qu*sqrt(obj.Ga)/(obj.Wp*cap(1));
                    resist(4)=resist(2);
                    resist(1)=1/(((obj.Wz*cap(1))^2)*resist(8)*resist(4)/resist(7));
                    resist(5)=((obj.Wp*cap(1))^2)*(resist(4)*resist(8)*resist(1));
                case {3, 7, 8}
                    zinp=value;

                    cap(1)=10e-9;
                    cap(2)=cap(1);

                    resist(3)=zinp;
                    resist(7)=zinp;
                    resist(8)=zinp;            
                    resist(6)=resist(8)*obj.Ga;

                    resist(2)=obj.Qu*sqrt(obj.Ga)/(obj.Wp*cap(1));
                    resist(4)=resist(2);
                    resist(1)=1/(((obj.Wz*cap(1))^2)*resist(8)*resist(4)/resist(7));
                    resist(5)=((obj.Wp*cap(1))^2)*(resist(4)*resist(8)*resist(1));
                case {9, 10}
                    zinp=47e3;

                    cap(1)=value;
                    cap(2)=value;

                    resist(3)=zinp;
                    resist(7)=zinp;
                    resist(8)=zinp;            
                    resist(6)=resist(8)*obj.Ga;

                    resist(2)=obj.Qu*sqrt(obj.Ga)/(obj.Wp*cap(1));
                    resist(4)=resist(2);
                    resist(1)=1/(((obj.Wz*cap(1))^2)*resist(8)*resist(4)/resist(7));
                    resist(5)=((obj.Wp*cap(1))^2)*(resist(4)*resist(8)*resist(1));
            end

            status = obj.validateComponents(resist,cap);
            
            if (status)
                obj.Resists = resist;
            	obj.Caps = cap;
            end
        end
    end
end

